from datetime import datetime
from typing import Optional, List, Union
from pydantic import BaseModel, Field
from fastapi import UploadFile


class SpeechRecognizeRequest(BaseModel):
    """
    语音识别请求模型
    """
    language: Optional[str] = Field(default='zh', description='语言类型：zh/en/auto')
    model_name: Optional[str] = Field(default='sensevoice-small', description='识别模型名称')
    use_vad: Optional[bool] = Field(default=True, description='是否使用语音活动检测')
    use_punc: Optional[bool] = Field(default=True, description='是否添加标点符号')
    use_itn: Optional[bool] = Field(default=True, description='是否使用逆文本归一化')
    hotwords: Optional[List[str]] = Field(default=None, description='热词列表')
    session_id: Optional[str] = Field(default=None, description='会话ID（用于实时识别）')
    api_key: Optional[str] = Field(default=None, description='API密钥（用于客户端认证）')
    callback_url: Optional[str] = Field(default=None, description='异步回调地址')


class SpeechRecognizeResponse(BaseModel):
    """
    语音识别响应模型
    """
    speech_id: Optional[int] = Field(description='语音记录ID')
    api_request_id: Optional[str] = Field(description='API请求ID')
    text: str = Field(description='识别结果文本')
    confidence: Optional[float] = Field(description='识别置信度')
    duration: Optional[float] = Field(description='音频时长（秒）')
    language: Optional[str] = Field(description='检测到的语言类型')
    model_name: Optional[str] = Field(description='使用的识别模型')
    status: int = Field(description='处理状态：0-处理中, 1-成功, 2-失败')
    user_type: int = Field(description='用户类型：1-系统用户, 2-客户端用户')
    callback_status: Optional[int] = Field(description='回调状态：0-未回调, 1-回调成功, 2-回调失败')
    create_time: Optional[datetime] = Field(description='创建时间')


class SpeechUploadRequest(BaseModel):
    """
    语音文件上传请求模型
    """
    file: UploadFile = Field(description='语音文件')
    language: Optional[str] = Field(default='zh', description='语言类型')
    model_name: Optional[str] = Field(default='sensevoice-small', description='识别模型名称')
    api_key: Optional[str] = Field(default=None, description='API密钥（用于客户端认证）')
    callback_url: Optional[str] = Field(default=None, description='异步回调地址')


class SpeechQueryModel(BaseModel):
    """
    语音识别记录查询模型
    """
    user_id: Optional[int] = Field(default=None, description='系统用户ID')
    client_user_id: Optional[int] = Field(default=None, description='客户端用户ID')
    user_type: Optional[int] = Field(default=None, description='用户类型：1-系统用户, 2-客户端用户')
    session_id: Optional[str] = Field(default=None, description='会话ID')
    api_request_id: Optional[str] = Field(default=None, description='API请求ID')
    status: Optional[int] = Field(default=None, description='处理状态：0-处理中, 1-成功, 2-失败')
    language: Optional[str] = Field(default=None, description='语言类型')
    model_name: Optional[str] = Field(default=None, description='识别模型')
    callback_status: Optional[int] = Field(default=None, description='回调状态')
    create_time_start: Optional[datetime] = Field(default=None, description='创建时间开始')
    create_time_end: Optional[datetime] = Field(default=None, description='创建时间结束')
    page_num: int = Field(default=1, description='页码')
    page_size: int = Field(default=10, description='每页大小')

    @classmethod
    def as_query(cls, **kwargs):
        """转换为查询参数"""
        return cls(**kwargs)


class RealtimeSpeechMessage(BaseModel):
    """
    实时语音识别消息模型
    """
    type: str = Field(description='消息类型：start/chunk/end/result/error')
    session_id: str = Field(description='会话ID')
    data: Optional[Union[str, bytes]] = Field(default=None, description='消息数据')
    text: Optional[str] = Field(default=None, description='识别文本')
    is_final: Optional[bool] = Field(default=False, description='是否为最终结果')
    confidence: Optional[float] = Field(default=None, description='置信度')
    timestamp: Optional[datetime] = Field(default_factory=datetime.now, description='时间戳')
    api_key: Optional[str] = Field(default=None, description='API密钥（用于客户端认证）')


class SpeechDeleteModel(BaseModel):
    """
    语音记录删除模型
    """
    speech_ids: List[int] = Field(description='语音记录ID列表')


class SpeechBatchQueryModel(BaseModel):
    """
    批量语音识别查询模型
    """
    api_request_ids: List[str] = Field(description='API请求ID列表')
    user_type: Optional[int] = Field(default=2, description='用户类型：2-客户端用户')


class SpeechConfigModel(BaseModel):
    """
    语音识别配置模型
    """
    use_vad: bool = Field(default=True, description='是否使用语音活动检测')
    use_punc: bool = Field(default=True, description='是否添加标点符号')
    use_itn: bool = Field(default=True, description='是否使用逆文本归一化')
    chunk_size: Optional[int] = Field(default=960, description='实时识别块大小')
    hotwords: Optional[List[str]] = Field(default=None, description='热词列表')
    custom_config: Optional[dict] = Field(default=None, description='自定义配置参数')


class SpeechModelInfo(BaseModel):
    """
    语音识别模型信息VO
    """
    model_name: str           # 模型名称，如 paraformer-zh-streaming
    language: str             # 支持的语言，如 zh/en
    description: str = ""     # 模型描述
    is_streaming: bool = True # 是否支持流式识别
    version: str = "1.0"      # 模型版本号 


class SpeechQuotaInfo(BaseModel):
    """
    语音识别配额信息VO
    """
    total_quota: int                      # 总配额（如每月可用次数/时长）
    used_quota: int                       # 已用配额
    remaining_quota: int                  # 剩余配额
    reset_time: Optional[str] = ""        # 重置时间（ISO格式字符串）
    unit: str = "times"                   # 配额单位（如 times/seconds/minutes）
    description: str = ""                 # 说明






    